<?php
/**
 * Created by PhpStorm.
 * User: liuyiwei@7659.com
 * Date: 2018/9/7
 * Time: 17:03
 */

namespace backend\helpers;


use backend\models\match\Match;
use backend\models\match\MatchUser;
use yii\base\Exception;

class ExportHelper
{
    /**
     * 导出报名表
     * @param $matchId
     * @param $pathname
     */
    public static function matchUser($matchId, $pathname)
    {
        $matchModel = Match::findOne(['id'=>$matchId]);
        if ($matchModel === null) {
            throw new Exception("请求参数校验失败！");
        }

        $objectPHPExcel = new \PHPExcel();
        $objectPHPExcel->setActiveSheetIndex(0);

        // 报表头的输出
        $objectPHPExcel->getActiveSheet()->mergeCells('A1:K1');
        $objectPHPExcel->getActiveSheet()->setCellValue('A1', '报名表');

        $objectPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(24);
        $objectPHPExcel->getActiveSheet()->getStyle('A1')
            ->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        $objectPHPExcel->getActiveSheet()->setCellValue('A2', '');
        $objectPHPExcel->getActiveSheet()->getStyle('G2')
            ->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);

        $objectPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(25);
        $objectPHPExcel->getActiveSheet()->setCellValue('A3', '序号');
        $objectPHPExcel->getActiveSheet()->setCellValue('B3', '用户ID');
        $objectPHPExcel->getActiveSheet()->setCellValue('C3', '游戏角色名');
        $objectPHPExcel->getActiveSheet()->setCellValue('D3', '战队注册ID');
        $objectPHPExcel->getActiveSheet()->setCellValue('E3', '战队名称');
        $objectPHPExcel->getActiveSheet()->setCellValue('F3', '提交时间');
        $objectPHPExcel->getActiveSheet()->setCellValue('G3', '报名提交人');

        // 数据
        $data = MatchUser::find()
            ->where([
                'match_id' => $matchId,
            ])
            ->orderBy('id asc')
            ->all();

        $row = 4;
        $i = 0;
        $lastTeamId = 0;
        if (!empty($data)) {
            foreach ($data as $key => $model) {
                if ($matchModel->flag == 2) {
                    $i = $key + 1;
                } else {
                    if ($model->team_id != $lastTeamId) {
                        $i++;
                    }
                }
                $objectPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
                $objectPHPExcel->getActiveSheet()->setCellValue('A' . $row, $i);
                $objectPHPExcel->getActiveSheet()->setCellValue('B' . $row, $model->player_id);
                $objectPHPExcel->getActiveSheet()->setCellValue('C' . $row, $model->role);
                $objectPHPExcel->getActiveSheet()->setCellValue('D' . $row, $model->team_id);
                $objectPHPExcel->getActiveSheet()->setCellValue('E' . $row, $model->team_name);
                $objectPHPExcel->getActiveSheet()->setCellValue('F' . $row, $model->created);
                $objectPHPExcel->getActiveSheet()->setCellValue('G' . $row, $model->created_by);
                $row++;
                $lastTeamId = $model->team_id;
            }
        }
        ob_end_clean();
        ob_start();

        // 保存
        if ($pathname == 'php://output') {
            header('Content-Type : application/vnd.ms-excel');
            header('Content-Disposition:attachment;filename="'.'报名表_'.$matchId.'-'.date("Y年m月j日").'.xlsx"');
        }
        $objWriter = \PHPExcel_IOFactory::createWriter($objectPHPExcel, 'Excel2007');
        $objWriter->save($pathname);
    }
}